package com.hl.ruoyilkshop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.ruoyilkshop.domain.GoodsSpecs;
import com.hl.ruoyilkshop.domain.vo.GoodsSpecsTreeVO;
import com.hl.ruoyilkshop.service.IGoodsSpecsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品规格Controller
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/goodspecs")
public class GoodsSpecsController extends BaseController
{
    @Autowired
    private IGoodsSpecsService goodsSpecsService;

    /**
     * 查询商品规格列表
     */
    @RequiresPermissions("lkshop:goodspecs:list")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSpecs goodsSpecs)
    {
        startPage();
        List<GoodsSpecs> list = goodsSpecsService.selectGoodsSpecsList(goodsSpecs);
        return getDataTable(list);
    }

    /**
     * 获取商品规格树形结构（包含规格值）
     */
    @RequiresPermissions("lkshop:goodspecs:list")
    @GetMapping("/tree")
    public AjaxResult getTree(GoodsSpecs goodsSpecs)
    {
        List<GoodsSpecsTreeVO> treeList = goodsSpecsService.selectGoodsSpecsTree(goodsSpecs);
        return success(treeList);
    }

    /**
     * 导出商品规格列表
     */
    @RequiresPermissions("lkshop:goodspecs:export")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSpecs goodsSpecs)
    {
        List<GoodsSpecs> list = goodsSpecsService.selectGoodsSpecsList(goodsSpecs);
        ExcelUtil<GoodsSpecs> util = new ExcelUtil<GoodsSpecs>(GoodsSpecs.class);
        util.exportExcel(response, list, "商品规格数据");
    }

    /**
     * 获取商品规格详细信息
     */
    @RequiresPermissions("lkshop:goodspecs:query")
    @GetMapping(value = "/{goodsSpecId}")
    public AjaxResult getInfo(@PathVariable("goodsSpecId") Integer goodsSpecId)
    {
        return success(goodsSpecsService.selectGoodsSpecsByGoodsSpecId(goodsSpecId));
    }

    /**
     * 新增商品规格
     */
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSpecs goodsSpecs)
    {
        int rows = goodsSpecsService.insertGoodsSpecs(goodsSpecs);
        if (rows > 0) {
            // 返回主键ID（配合 Mapper 的 useGeneratedKeys + LAST_INSERT_ID，在重复时也能拿到已存在ID）
            return success(goodsSpecs.getGoodsSpecId());
        }
        return error("新增规格失败");
    }

    /**
     * 修改商品规格
     */
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSpecs goodsSpecs)
    {
        return toAjax(goodsSpecsService.updateGoodsSpecs(goodsSpecs));
    }

    /**
     * 删除商品规格
     */
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsSpecIds}")
    public AjaxResult remove(@PathVariable Integer[] goodsSpecIds)
    {
        return toAjax(goodsSpecsService.deleteGoodsSpecsByGoodsSpecIds(goodsSpecIds));
    }
}
